/**
 * Created by ls.c on 2016/11/3.
 */

import {getjson, postjson, putjson, deljson} from '../../requestJson'

/**
 * 用户登录状态
 */
/*
export const fetchLoginStatus = ({commit}, names) => {

	commit('LOADING_STATUS', names)

}
*/

/**
 * 登录
 * http://test.iot.idao.me/login?username=sale1&password=test01
 */
export const fetchToken = ({ commit }, params) =>{

	const url = 'token/authorize'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				localStorage.setItem("tokenKey", json.result.token);

				return commit('TOKEN_KEY', json)
			}
			return commit('TOKEN_KEY', json)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
export const fetchLogin = ({ commit }, params) =>{

	const url = 'login'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				return commit('LOADING_STATUS', json)
			}
			return commit('LOADING_STATUS', json)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 退出登录
 * http://test.iot.idao.me/logout
 */
export const fetchLogout = ({ commit }, params) =>{

	const url = 'logout'
	const query =  params
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				return commit('LOADING_STATUS', !json.success)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 用户信息
 */
export const fetchProfile = ({ commit }, params) =>{

	const url = 'profile'
	const query =  params
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				localStorage.setItem("role", json.result.role);

				return commit('FETCH_PROFILE_SUCCESS', json.result)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 修改密码
 */
export const fetchChangePwd = ({ commit }, params) =>{

	const url = 'changePwd'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				return commit('LOADING_STATUS', json)
			}
			return commit('LOADING_STATUS', json)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/******************************************** base api end ********************************************/

/**
 * 获取所有菜单信息
 */
export const fetchMenu = ({ commit }, ids) =>{

	const url = 'menu'
	const query =  ids
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				return commit('FETCH_MENU_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 获取所有移动卡信息
 */
export const fetchCardCMCC = ({ commit }, params) =>{

	const url = 'overlayPacket/list'
	const query =  `pageIndex=${params.page}&pageSize=10&iccid=${params.iccid}&activetimeStart=${params.activetimeStart}&activetimeEnd=${params.activetimeEnd}&status=${params.status}&packagecode=${params.packagecode}&datasurplus=${params.datasurplus}&customerId=${params.customerId}`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_CARD_CMCC_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 获取所有移动卡卡数量统计
 */
export const fetchCardStatistics = ({ commit }, params) =>{

	const url = 'overlayPacket/cardStatistics'
	const query =  ``
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_CARD_CMCC_TOTAL_SUCCESS', json.result)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 获取叠加包
 */
export const fetchCardPackage = ({ commit }, params) =>{

	const url = 'overlayPacket/packageList'
	const query =  `secondaryId=${params}`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_CARD_PACKAGE_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 新增叠加包
 */
export const fetchPackageAdd = ({ commit }, params) =>{

	const url = 'overlayPacket/insert'
	const query = params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return alert('添加成功')
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}



/**
 * 获取所有订单信息
 */
export const fetchOrderList = ({ commit }, params) =>{

	const url = 'mainOrder/list'
	const query =  `operatorId=${params.operatorId}&orderDate=${params.orderDate}&orderId=${params.orderId}&packageName=${params.packageName}`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_ORDER_LIST_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 删除订单
 */
export const fetchOrderDel = ({ commit }, params) =>{

	let listParams = {
		operatorId: '', //	运营商ID	number
		orderDate: '', //	订购日期	string	格式yyyy-MM-dd
		orderId: '',	// 订单号	number
		packageName: '' //	销售套餐名称	string
	}
	const url = 'mainOrder/delete'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				fetchOrderList({ commit }, listParams)
				return alert('删除成功')
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 查询主订单
 */
export const fetchOrderSearch = ({ commit }, params) =>{

	const url = 'mainOrder/list'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				return commit('FETCH_ORDER_LIST_SUCCESS', json)
			}
			return commit('FETCH_RESULT_MSG', json)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 新增订单
 */
export const fetchOrderAdd = ({ commit }, params) =>{

	const url = 'mainOrder/insert'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				return alert("订单新增成功！")
			}
			return commit('FETCH_RESULT_MSG', json)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 获取每条订单信息
 */
export const fetchOrderInfo = ({ commit }, params) =>{

	const url = 'secondaryOrder/list'
	const query =  `mainId=${params.mainId}&pageIndex=${params.page}&pageSize=10`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_ORDER_INFO_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 获取所有叠加包订单信息
 */
export const fetchOrderPackage = ({ commit }, params) =>{

	if(!params.id) params.id = ''
	if(!params.packageName) params.packageName = ''
	if(!params.orderDate) params.orderDate = ''
	const url = 'overlayPacket/orderList'
	const query =  `pageIndex=${params.page}&pageSize=10&id=${params.id}&packageName=${params.packageName}&orderDate=${params.orderDate}`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_ORDER_PACKET_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}


/**
 * 获取运营商列表
 */
export const fetchOrderOperator = ({ commit }, ids) =>{

	const url = 'api/operator/list'
	const query =  ids
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_ORDER_OPERATOR_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 获取卡类型列表
 */
export const fetchOrderCardType = ({ commit }, ids) =>{

	const url = 'cardType/list'
	const query = `operatorId=${ids}`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_ORDER_CARD_TYPE_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}




/**
 * 获取账单信息
 */
export const fetchBill = ({ commit }, params) =>{

	if(!params.year) params.year = ''
	if(!params.months) params.months = ''
	const url = 'bill/billInfo'
	const query = `pageIndex=${params.page}&pageSize=10&years=${params.year}&months=${params.months}`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_BILL_LIST_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 获取账单金额统计
 */
export const fetchBillStatistics = ({ commit }, params) =>{

	const url = 'bill/billStatistics'
	const query = ''
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_BILL_STATISTICS_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}


/**
 * 获取账单信息(客户)
 */
export const fetchGeustBill = ({ commit }, params) =>{

	const url = 'bill/geustBillInfo'
	const query = `pageIndex=${params.page}&pageSize=10&years=${params.year}&months=${params.months}&customerId=${params.customerId}`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_GEUST_BILL_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 获取账单金额统计(客户)
 */
export const fetchGeustBillStatistics = ({ commit }, params) =>{

	const url = 'bill/geustBillStatistics'
	const query = ''
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_GEUST_BILL_STATISTICS_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}


/**
 * 获取帐号列表
 */
export const fetchAccount = ({ commit }, params) =>{

	const url = 'account/list'
	const query =  `pageIndex=${params.page}&pageSize=10`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				return commit('FETCH_ACCOUNT_LIST_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 获取单个帐号信息
 */
export const fetchAccountInfo = ({ commit }, params) =>{

	const url = 'account/get'
	const query =  `username=${params.name}`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_ACCOUNT_INFO_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 新增帐号
 */
export const fetchAccountAdd = ({ commit }, params) =>{

	const url = 'account/add'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				fetchAccount({ commit }, {page: 1})
				return alert(json.result)
			}
			return alert(json.resultMsg)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 修改帐号
 */
export const fetchAccountEdit = ({ commit }, params) =>{

	const url = 'account/edit'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				fetchAccount({ commit }, {page: 1})
				return alert(json.result)
			}
			return alert(json.resultMsg)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 冻结公司内帐号
 */
export const fetchAccountDel = ({ commit }, params) =>{

	const url = 'account/del'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				fetchAccount({ commit }, {page: 1})
				return alert(json.result)
			}
			return alert(json.resultMsg)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}


/**
 * 冻结客户帐号
 */
export const fetchGeustAccountDel = ({ commit }, params) =>{

	const url = 'client/del'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				fetchAgent({ commit }, {page: 1})
				return alert(json.result)
			}
			return alert(json.resultMsg)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}


/**
 * 获取客户信息
 */
export const fetchAgent = ({ commit }, params) =>{

	const url = 'client/list'
	const query = `pageIndex=${params.page}&pageSize=10`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_AGENT_LIST_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 获取单个客户信息
 */
export const fetchAgentInfo = ({ commit }, params) =>{

	const url = 'client/get'
	const query =  `username=${params.name}`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_AGENT_INFO_SUCCESS', json)
			}
			return alert(json.resultMsg)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 新增客户
 */
export const fetchAgentAdd = ({ commit }, params) =>{

	const url = 'client/add'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				fetchAgent({ commit }, {page: 1})
				return alert(json.result)
			}
			return alert(json.resultMsg)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 编辑客户
 */
export const fetchAgentEdit = ({ commit }, params) =>{

	const url = 'client/edit'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				fetchAgent({ commit }, {page: 1})
				return alert(json.result)
			}
			return alert(json.resultMsg)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}




/**
 * 获取首页公告信息
 */
export const fetchHomeNotice = ({ commit }, params) =>{

	const url = 'notice/list'
	const query = `pageIndex=1&pageSize=900`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_NOTICE_LIST_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 获取公告信息
 */
export const fetchNotice = ({ commit }, params) =>{

	const url = 'notice/list'
	const query = `pageIndex=${params.page}&pageSize=10`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_NOTICE_LIST_SUCCESS', json)
			}
			return Promise.reject(new Error(json.success))
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 新增公告信息
 */
export const fetchNoticeAdd = ({ commit }, params) =>{

	const url = 'notice/add'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
					alert('新增公告成功')
					return
			}
			return alert('新增公告失败')
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 删除公告信息
 */
export const fetchNoticeDel = ({ commit }, params) =>{

	const url = 'notice/delete'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				fetchNotice({ commit }, {page: 1})
				alert('删除成功')
				return
			}
			return commit('FETCH_RESULT_MSG', json.resultMsg)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}
/**
 * 移动卡入库
 */
export const fetchCmccInCard = ({ commit }, params) =>{

	const url = 'card/upload/cmcc'
	const query =  params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				return commit('FETCH_CMCC_INCARD_SUCCESS', json.result)
			}
			return alert(json.resultMsg)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}


/**
 * 获取能力平台信息
 */
export const fetchCarrierInfo = ({ commit }, params) =>{

	const url = 'carrier/list'
	const query = `pageIndex=${params.page}&pageSize=10`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_CARRIER_INFO_SUCCESS', json)
			}
			return alert(json.resultMsg)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}


/**
 * 获取能力平台信息(编辑)
 */
export const fetchCarrierItem = ({ commit }, params) =>{

	const url = 'carrier/get'
	const query = `id=${params.id}`
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_CARRIER_ITEM_SUCCESS', json)
			}
			return alert(json.resultMsg)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 新增能力平台信息
 */
export const fetchCarrierAdd = ({ commit }, params) =>{

	const url = 'carrier/add'
	const query = params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				fetchCarrierInfo({ commit }, {page: 1})
				alert('新增能力平台成功')
				return
			}
			return alert('新增能力平台失败')
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 修改能力平台信息
 */
export const fetchCarrierEdit = ({ commit }, params) =>{

	const url = 'carrier/edit'
	const query = params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				fetchCarrierInfo({ commit }, {page: 1})
				alert('修改能力平台成功')
				return
			}
			return alert('修改能力平台失败')
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 删除能力平台信息
 */
export const fetchCarrierDel = ({ commit }, params) =>{

	const url = 'carrier/del'
	const query = params
	return postjson({url, query}, commit)
		.then((json) => {
			if (json.success) {
				fetchCarrierInfo({ commit }, {page: 1})
				alert('删除能力平台成功')
				return
			}
			return alert('删除能力平台失败')
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

/**
 * 获取能力平台信息
 */
export const fetchCarrierAlias = ({ commit }, params) =>{

	const url = 'carrier/alias'
	const query = ``
	return getjson({url, query}, commit)
		.then((json) => {
			if (json.success) {

				return commit('FETCH_CARRIER_ALIAS_SUCCESS', json)
			}
			return alert(json.resultMsg)
		})
		.catch((error) => {
			return Promise.reject(error)
		})

}

